import urllib.parse
import telebot
import requests

bot = telebot.TeleBot('7840326858:AAF_1yFkLX2PKv6WYwMUVm7RL9ck1tdPcbI')

@bot.message_handler(commands=['start'])
def start_message(message):
    print('start_message:', message)
    # bot.reply_to(message, '你好')
    bot.send_message(message.chat.id, '我是崔大师，欢迎光临！')


@bot.message_handler(func=lambda message: True)
def echo_all(message):
    print('echo_all:', message)
    # bot.reply_to(message, message.text)
    try:
        encoded_text = urllib.parse.quote(message.text, )
        print('encoded_text:', encoded_text)
        response = requests.post(f'http://127.0.0.1:8001/chat?query={encoded_text}', timeout=100)
        if response.status_code == 200:
            print('response:', response)
            print('response.json():', response.json())
            answer = response.json()['output']
            bot.send_message(message.chat.id, answer)
        else:
            bot.send_message(message.chat.id, '我不知道你在说什么')
    except Exception as e:
        print('e:', e)
        bot.send_message(message.chat.id, '我不知道你在说什么')

bot.infinity_polling()